<div class="layui-card">
  <script type="text/html" template lay-done="layui.data.done(d.params)">
    <div class="layui-card-body">
      <div id="table_asset_tokens" lay-filter="table_asset_tokens"></div>
    </div>
  </script>
</div>
<script type="text/html" id="tpl_lock_user">
  {{d.user.mobile}}
</script>
<script type="text/html" id="tpl_product_name">
  {{d.product.name}}
</script>
<script type="text/html" id="tpl_cancel_tool">
  <a class="layui-table-link" lay-event="batch_cancel">
    批量取消
  </a>
</script>

<script>
  layui.data.done = function (params) {
    let mobile = params.mobile
      , contract_address = params.contract_address
    layui.use([], function () {
      let $ = layui.$
        , form = layui.form
        , table = layui.table
        , admin = layui.admin
        , view = layui.view

      let resourceUrl = 'agent-admin/business/user_asset_tokens'

      // 列表查询
      table.render({
        elem: '#table_asset_tokens'
        , url: resourceUrl
        , where: {mobile, contract_address}
        , page: true
        , cols: [[
          {title: '转售价格', field: 'display_price', minWidth: 100}
          , {title: '可取消数量', field: 'free_count', width: 100}
          , {title: '锁定数量', field: 'lock_count', width: 100}
          , {title: '操作', width: 150, align: 'left', fixed: 'right', toolbar: '#tpl_cancel_tool'}
        ]]
      });


      //监听操作栏
      table.on('tool(table_asset_tokens)', function (obj) {
        let data = obj.data;
        data.mobile = mobile
        data.contract_address = contract_address
        if (obj.event === 'batch_cancel') {
          showCancelForm(data)
        }
      });

      //批量取消转售弹窗
      function showCancelForm(data) {
        let minWidth = '450px';
        let minHeight = '300px';
        data.mobile = layui.router().search.mobile
        admin.popup({
          title: '批量取消转售'
          , area: [minWidth, minHeight]
          , id: 'popup_batch_cancel_resale'
          , success: function (layero, index) {
            view(this.id).render('business/cancel_resale_form', data).done(function () {
              form.on('submit(submit_cancel_resale)', function (d) {
                let field = d.field;
                if (field.cancel_count > data.free_count) {
                  layer.msg("取消数量不能大于" + data.free_count)
                  return false;
                }
                $('#submit_resale').attr('disabled', true).val('请求处理中，请耐心等待');
                admin.post("agent-admin/business/batch_cancel_resale", field, function (res) {
                  layer.close(index);
                  table.reload('table_asset_tokens');
                  table.reload('table_resale_product');
                })
              })
            })
          }
        })
      }
    })
  }
</script>

